package com.example.userservice.controller;


import com.example.chatservice.webSocket.ChannelContextUtils;
import com.example.userservice.service.UserInfoService;
import com.example.userservice.rabbitmq.publisher.UserPublisher;
import org.example.annotation.GlobalInteceptor;
import org.example.config.redis.RedisUtils;
import org.example.controller.ABaseController;
import org.example.entity.constants.Constants;
import org.example.entity.dto.TokenUserInfoDto;
import org.example.entity.enums.ResponseCodeEnum;
import org.example.entity.po.UserInfo;
import org.example.entity.vo.ResponseVO;
import org.example.entity.vo.UserInfoVO;
import org.example.exception.BusinessException;
import org.example.utils.StringTools;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * @Description
 * @Author susu
 * @Date 2024/9/8
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends ABaseController {

    @Resource
    private ChannelContextUtils channelContextUtils;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserPublisher userPublisher;
    @Resource
    private RedisUtils redisUtils;
    @RequestMapping("/getUserInfo")
    @GlobalInteceptor
    public ResponseVO getUserInfo(HttpServletRequest request){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserInfo userInfoByUserId = userInfoService.getUserInfoByUserId(tokenUserInfo.getUserId());
        if (userInfoByUserId == null){
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        UserInfoVO vo = new UserInfoVO();
        BeanUtils.copyProperties(userInfoByUserId,vo);
        vo.setAdmin(tokenUserInfo.getAdmin());
        return getSuccessResponseVO(vo);
    }

    @RequestMapping("/saveUserInfo")
    @GlobalInteceptor
    public ResponseVO saveUserInfo(HttpServletRequest request,
                                   @NotNull MultipartFile avatarFile,
                                   @NotNull MultipartFile avatarCover,
                                   UserInfo userInfo){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

        try {
            userInfoService.saveUserInfo(userInfo,tokenUserInfo.getUserId(),avatarFile,avatarCover);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return getUserInfo(request);
    }

    @RequestMapping("/updatePassword")
    @GlobalInteceptor
    public ResponseVO updatePassword(HttpServletRequest request,
                                   @NotNull String password){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserInfo info = userInfoService.getUserInfoByUserId(tokenUserInfo.getUserId());
        if (info == null){
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        info.setPassword(StringTools.encodeByMD5(password));
        userInfoService.updateUserInfoByUserId(info,tokenUserInfo.getUserId());

        redisUtils.delete(Constants.REDIS_KEY_WS_TOKEN+tokenUserInfo.getToken());
//        channelContextUtils.closeContext(tokenUserInfo.getUserId());
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/logout")
    @GlobalInteceptor
    public ResponseVO logout(HttpServletRequest request){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        redisUtils.delete(Constants.REDIS_KEY_WS_TOKEN+tokenUserInfo.getToken());
        channelContextUtils.removeContext(tokenUserInfo.getUserId());
        return getSuccessResponseVO(null);
    }
}
